/*******************************
	平台移动端公用
	@author:shanghang
	@date:2016-11-1
*******************************/
'use strict'
/***********响应式计算计算**************/
!function(e,t){
	var mresize=function(){
		var view_width = document.getElementsByTagName('html')[0].getBoundingClientRect().width;
		var n = Math.min(1200,view_width);
		return n?void (t.documentElement.style.fontSize=n/15+"px"):!1;
	};
	mresize();
	e.addEventListener("resize",mresize,!1);
	e.addEventListener("DOMContentLoaded",mresize,!1);
	e.addEventListener("load",mresize,!1);
}(window,document);
var hastouch = "ontouchstart" in window?true:false;
var touchstart = hastouch?"touchstart":"mousedown";
var touchmove = hastouch?"touchmove":"mousemove";
var touchend = hastouch?"touchend":"mouseup";
//vue修改文本插值的定界符
//Vue.config.delimiters = ['[[', ']]'];
//开启错误提示
//Vue.config.debug = true;
window.Bee = {};
;(function(bee){
	var domain = 'http://m.9tech.cn';
	var credentials = 'include';
	var mode = 'cors';//"same-origin"
	//token
	var ipt = document.createElement('input');
	ipt.type = 'hidden';
	ipt.name = 'token';
	ipt.value = getCookie('PHPSESSID');
	
	bee.getjson = function(url, fn, load){
		load || (loadDom.style.display = 'block');
		//url上加 token
		fetch(domain + url, {mode: mode, credentials: credentials}).then(function(response) {
			loadDom.style.display = 'none';
	    	return response.json()
		}).then(function(json) {
			loadDom.style.display = 'none';
		    fn && fn();
		}).catch(function(ex) {
			loadDom.style.display = 'none';
		    console.log('parsing failed', ex)
		    alert('err');
		})
	};
	
	bee.subform = function(url, form, fn, load){
		load || (loadDom.style.display = 'block');
		form.appendChild(ipt);
		fetch(domain + url, {
			method: 'POST',
			mode: mode,
			credentials : credentials,
			body: new FormData(form)
		}).then(function(response) {
			loadDom.style.display = 'none';
	    	return response.json()
		}).then(function(json) {
			loadDom.style.display = 'none';
			if(json.code == '10003'){
				localStorage.isLogin = false;
				location.href = './personal.html';
				return false;
			};
			localStorage.isLogin = true;
			if(json.code != 0){
				alert(json.message);
				return false;
			};
		    fn && fn(json.data);
		})
	};
	
	bee.get = function(url, param, fn, load){
		fetch(domain + url, {
		    method: 'get'
		}).then(function(response) {
			return response.json();
		}).then(function(json) {
		    if(json.code == '10003'){
				localStorage.isLogin = false;
				location.href = './personal.html';
				return false;
			};
			localStorage.isLogin = true;
			if(json.code != 0){
				alert(json.message);
				return false;
			};
		    fn && fn(json.data);
		});
	};

	bee.post = function(url, param, fn, load){
		load || (loadDom.style.display = 'block');
		param.token = ipt.value;
		fetch(domain + url, {
			method: 'POST',
			mode: mode,
			credentials : credentials,
			headers: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"},
			body: paramToString(param)//
		}).then(function(response) {
			loadDom.style.display = 'none';
	    	return response.json();
		}).then(function(json) {
			loadDom.style.display = 'none';
			if(json.code == '10003'){
				localStorage.isLogin = false;
				location.href = './personal.html';
				return false;
			};
			localStorage.isLogin = true;
			if(json.code != 0){
				alert(json.message);
				return false;
			};
		    fn && fn(json.data);
		})
	};
	
	bee.getUserStatus = function(fn){
		fetch(domain +   '/user/info', {
			method: 'POST',
			mode: mode,
			credentials : credentials,
			headers: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"},
			body: 'token='+ipt.value//
		}).then(function(response) {
	    	return response.json();
		}).then(function(json) {
			var username = '';
			if(json.code == 0){
				username = json.data.info.username;
				localStorage.isLogin = true;
			}else{
				localStorage.isLogin = false;
			}
		    fn && fn(username);
		})
	};
	
	var paramToString = function(param, key){
		var paramStr="";
		if(typeof param === "string"||typeof param === "number"||typeof param === "boolean"){
			paramStr+="&"+key+"="+encodeURIComponent(param);
		}else{
			for(var i in param){
				var k=key==null?i:key+(param instanceof Array?"["+i+"]":"."+i);
				paramStr+='&'+paramToString(param[i], k);
			}
		}
		return paramStr.substr(1);
	};
	
	//getcookie
	function getCookie(name) { 
	    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	    if(arr=document.cookie.match(reg))
	        return unescape(arr[2]); 
	    else 
	        return null; 
	} 
	
	//loading
	var loadDom = document.createElement('div');
	loadDom.className = 'loadingbg';
	loadDom.innerHTML = '<div class="loader"></div>';
	setTimeout(function(){
		document.body.appendChild(loadDom);
	},30);
})(Bee);


//html
//fetch('/users.html')
//.then(function(response) {
//  return response.text()
//}).then(function(body) {
//  document.body.innerHTML = body
//})
				
//form
//var form = document.querySelector('form')
//



//post
//fetch('/users', {
//method: 'POST',
//headers: {
//  'Content-Type': 'application/json'
//},
//body: JSON.stringify({
//  name: 'Hubot',
//  login: 'hubot',
//})
//})

//上传
//var input = document.querySelector('input[type="file"]')
//
//var data = new FormData()
//data.append('file', input.files[0])
//data.append('user', 'hubot')
//
//fetch('/avatars', {
//method: 'POST',
//body: data
//})

//function checkStatus(response) {
//if (response.status >= 200 && response.status < 300) {
//  return response
//} else {
//  var error = new Error(response.statusText)
//  error.response = response
//  throw error
//}
//}
//
//function parseJSON(response) {
//return response.json()
//}

//		console.log(response.headers.get('Content-Type'))
//console.log(response.headers.get('Date'))
//console.log(response.status)
//console.log(response.statusText)